import 'package:lib_device/lib_device.dart';

/// 屏幕适配工具
class ScreenUtils {
  static double _designWidth = 667.0;
  static double _designHeight = 375.0;

  static late double screenWidth;
  static late double screenHeight;
  static late double screenRatio;

  static double get designWidth => _designWidth;

  static double get designHeight => _designHeight;

  static init(double width, double height) {
    _designWidth = width;
    _designHeight = height;
    screenWidth = DeviceUtils.screenWidth;
    screenHeight = DeviceUtils.screenHeight;
    screenRatio = DeviceUtils.screenRatio;
  }
}

/// 屏幕尺寸扩展、(考虑不同屏幕尺寸的手机，平板，电脑展示)
///
/// 关键参数：
/// 1，设计图尺寸
/// 2，屏幕尺寸
/// 3，屏幕基数比
/// 4，屏幕比
extension DoubleSizeExtension on num {
  /// 宽度
  double get x {
    // 屏幕尺寸信息
    double screenWidth = ScreenUtils.screenWidth;
    double screenRatio = ScreenUtils.screenRatio;
    double baseWidth = screenWidth / screenRatio;
    // 设计图尺寸比
    double multiple = ScreenUtils.designWidth / baseWidth;
    return this / multiple;
  }

  /// 高度
  double get y {
    // 屏幕尺寸信息
    double screenHeight = ScreenUtils.screenHeight;
    double screenRatio = ScreenUtils.screenRatio;
    double baseHeight = screenHeight / screenRatio;
    // 设计图尺寸比
    double multiple = ScreenUtils.designHeight / baseHeight;
    return this / multiple;
  }

  /// 屏幕百分比宽度
  double get sw {
    // 屏幕尺寸信息
    double screenWidth = ScreenUtils.screenWidth;
    double screenRatio = ScreenUtils.screenRatio;
    double baseWidth = screenWidth / screenRatio;
    return baseWidth * this;
  }

  /// 屏幕百分比高度
  double get sh {
    // 屏幕尺寸信息
    double screenHeight = ScreenUtils.screenHeight;
    double screenRatio = ScreenUtils.screenRatio;
    double baseHeight = screenHeight / screenRatio;
    return baseHeight * this;
  }

  /// 文字大小
  double get sp {
    double screenWidth = ScreenUtils.screenWidth;
    double screenRatio = ScreenUtils.screenRatio;
    double baseWidth = screenWidth / screenRatio;
    // 设计图尺寸比
    double multiple = ScreenUtils.designWidth / baseWidth;
    // LogUtils.v("screenWidth:$screenWidth, screenRatio:$screenRatio, baseWidth:$baseWidth, multiple:$multiple, this:$this");
    return this / multiple;
  }
}
